package com.wenhai.spring.event;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.PayloadApplicationEvent;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @ClassName ApplicationEventDemo
 * @Description
 * @Author 谢文海
 * @Date 2021/4/19 21:59
 * @Version 1.0
 **/
public class ApplicationEventDemo implements ApplicationEventPublisherAware {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(ApplicationEventDemo.class);
        context.addApplicationListener((ApplicationListener<PayloadApplicationEvent>) System.out::println);
        context.refresh();
        context.close();
    }


    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        applicationEventPublisher.publishEvent("hello, world");
    }

}
